<?php
/**
*
*	初始化程序 @zairwolf
*
*/

error_reporting(E_ALL);
define('ROOT', str_replace('\\', '/', dirname(dirname(__FILE__))).'/');
define('IN', true);

$mtime = explode(' ', microtime());
$starttime = $mtime[1] + $mtime[0];

define('TIMESTAMP', time());

define('MAGIC_QUOTES', get_magic_quotes_gpc());
set_magic_quotes_runtime(0);

require_once ROOT.'include/fso.func.php';
require_once ROOT.'include/string.func.php';

require_once ROOT.'include/global.func.php';
require_once ROOT.'include/Template.class.php';
require_once ROOT.'include/db.func.php';
require_once ROOT.'include/config.func.php';
require_once ROOT.'include/cache.func.php';

$_SYSTEM = $Global = $_CACHE = array();

require_once ROOT.'config.inc.php';

//初始化网站
global $db;
$db = null;
$querynum=0;
db_conn();

include _config('system');
include _config('safe');
include _config('user');
include _config('display');

//安全文件设定
define('SITE_SAFE_STATE', ROOT."data/close.state");

ob_start();

//约定变量定义
$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$_G['user_ip'] = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);

$sqladd = '';
if(isset($_CACHE['banlist']['ip']) && ipAccess($_CACHE['banlist']['ip'])) exit('很抱歉，您已被禁止访问本站');

db_q('DELETE FROM 3h_session WHERE lasttime < '.(TIMESTAMP-900));//900s的临时session过期


$sid = _cookie('sid');

$session_exists = false;
if($sid) {
	if($_t=db_r("SELECT * FROM 3h_session WHERE sid='".md5($sid)."'")) {
		$session_exists = true;
	}
}

$_G['user_id']	= 0;
$_G['user_name']= '游客';
$_G['user_secode']= '';
if(!$session_exists) {
	if(!$sid) $sid = md5($_G['user_ip'].rrand(20));

	$lnfo = _cookie('lnfo');
	if($lnfo && 2 == count($lnfo)) {
		list($_u, $_p) = $lnfo;
		$_t = db_r("SELECT * FROM 3h_user WHERE active=1 AND name='$_u' AND pswd='".md5($_p)."'");
		if($_t) {
			$_G['user_id']		= $_t['id'];
			$_G['user_name']	= $_t['name'];
			$_G['user_secode']	= '';
		}
	}

}else {
	$_G['user_id']		= $_t['user_id'];
	$_G['user_name']	= $_t['user_name'];
	$_G['user_secode']	= $_t['secode'];
}

db_q("REPLACE INTO 3h_session (sid,ip,user_id,user_name,lasttime,secode) VALUES ('".md5($sid)."','$_G[user_ip]','$_G[user_id]','$_G[user_name]', '".TIMESTAMP."','$_G[user_secode]')");

//记录最近活动时间
if($_G['user_id']>0 && !defined('IN_ACP')){
	db_q("update 3h_user set lastdateline='".TIMESTAMP."' where id='$_G[user_id]'");
}

cookie('sid', $sid, 900);

if(banList($_G['user_name'], 'username')) b('您的用户名已被封禁');//用户名已被封禁

if(file_exists(SITE_SAFE_STATE) && !defined('IN_ACP')) b($_SYSTEM['SYSTEM']['message']);

include _config('seo');

require_once ROOT.'include/header.inc.php';

$_tdtime = rtodaytime();

$action = _get_post('action');
$submit = _post('submit');
$inajaj = _get('_');//是否ajax
$json=array();
